package com.yaosheng.thread.fiber;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.strands.SuspendableRunnable;

/**
 * Created by yaosheng on 2021/7/27.
 * 阡程和线程对比Demo
 */
public class HelloFiber {

    public static void main(String[] args) {

        long start = System.currentTimeMillis ();
        for(int k = 0;k < 10000;k ++){

            Fiber<Void> fiber = new Fiber (new SuspendableRunnable () {
                @Override
                public void run() throws SuspendExecution, InterruptedException {
                    calc ();
                }
            });

            fiber.start ();

            Thread thread = new Thread (new Runnable () {
                @Override
                public void run() {
                    calc ();
                }
            });
            thread.start ();
        }

        long end = System.currentTimeMillis ();
        System.out.println (end - start);
    }

    public static void calc(){

        int result = 0;
        for (int m = 0;m < 10000;m ++){
            for(int i = 0;i < 200;i ++){
                result += i;
            }
        }
    }
}
